package com.united.ucts.modules.monitor.controller;

import com.united.frame.common.exception.UnitedException;
import com.united.frame.common.module.controller.BaseController;
import com.united.frame.plugin.restfull.response.ResponseResultBody;
import com.united.ucts.modules.monitor.pojo.ServerInfo;
import com.united.ucts.modules.monitor.service.ServerMonitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 获取服务运行服务器监控信息前端控制器类
 *
 * @author hudan
 * @version 1.0
 * @filename ServerMonitorController.java
 * @copyright www.united.com
 * @date 2019/7/22
 */
@Slf4j
@Api(value = "监控-系统服务监控", tags = {"获取服务运行情况监控信息"})
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "${request.mapping}/monitor/server")
public class ServerMonitorController extends BaseController {

    final ServerMonitorService serverMonitorService;

    /**
     * 页面获取服务器信息
     *
     * @return 服务器信息
     */
    @ApiOperation(value = "页面获取服务器信息", notes = "页面获取服务器信息")
    @PostMapping(value = "/index")
    @ResponseResultBody
    public ServerInfo index() {

        // 获取服务器信息
        ServerInfo server = serverMonitorService.getServer();

        if (null != server) {
            return server;
        } else {
            return new ServerInfo();
        }
    }

    /**
     * 页面定时获取服务器信息
     *
     * @return 服务器信息
     */
    @ApiOperation(value = "定时获取服务器信息", notes = "定时获取服务器信息")
    @PostMapping(value = "/server")
    @ResponseResultBody
    public ServerInfo getServer() {

        try {
            // 获取服务器信息
            return serverMonitorService.getServer();
        } catch (Exception e) {
            log.error("获取服务器监控报错，错误原因:{}", e.getMessage());
            throw new UnitedException("获取服务器信息监控出错!");
        }
    }
}